import { AxiosRequestConfig, AxiosInstance, AxiosResponse } from "axios";

/**
 * 通过输入一个AxiosInstance
 *
 */
class RequestHandler {
  public instance: AxiosInstance;

  constructor(axiosInstance: AxiosInstance) {
    this.instance = axiosInstance;
  }

  public createGetRequestExecutor<T>(
    url: string,
    params: any
  ): () => Promise<AxiosResponse<T>> {
    const requestExecutor = async (): Promise<AxiosResponse<T>> => {
      try {
        const response: AxiosResponse<T> = await this.instance.get(url, {
          params,
        });
        return response;
      } catch (error) {
        throw new Error(`Request failed: ${error.message}`);
      }
    };

    return requestExecutor;
  }
}

export { RequestHandler };
export type { AxiosRequestConfig };
